package net.lwf.handler.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.string.util.StringManipulator;

import lwf.handler.entities.WordImage;


public class ImageFileFolderReader {
	
	//TODO
	//1. Remove all comments
	//2. Consider base directory images
	//3. Ignore directories
	
	/**
	 * Recursively walk a directory tree and return a List of all
	 * Files found; the List is sorted using File.compareTo().
	 *
	 * @param aStartingDir is a valid directory, which can be read.
	 */
	public static Map getMappedFileListing(File aStartingDir) {
			//throws FileNotFoundException {
		validateDirectory(aStartingDir);
		Map mapfiles = new HashMap();
		getMappedFileListing(aStartingDir, mapfiles);
		return mapfiles;
	}
	
	// PRIVATE //
//	private static List getFileListingNoSort(File aStartingDir)
//			throws FileNotFoundException {
//		Map map = new HashMap();
//		
//		List result = new ArrayList();
//		File[] filesAndDirs = aStartingDir.listFiles();
//		List filesDirs = Arrays.asList(filesAndDirs);
//		for (int i = 0; i < filesDirs.size(); i++) {
//			File file = (File) filesDirs.get(i);
//			String filename = file.getPath();
//			int pos = filename.indexOf("image");
//			String correctedimage = filename.substring(pos);
//
//			if(filterImage(correctedimage)) {
//				result.add(new WordImage(filename, correctedimage)); //always add, even if directory	
//			}
//			
//			if (!file.isFile()) {
//				//must be a directory
//				//recursive call!
//				List deeperList = getFileListingNoSort(file);
//				result.addAll(deeperList);
//			}
//		}
//		return result;
//	}
	
	private static Map getFileListingNoSort(File aStartingDir) {
			//throws FileNotFoundException {
		Map map = new HashMap();
		
		//System.setProperty("file.separator", "/");

		//List result = new ArrayList();
		File[] filesAndDirs = aStartingDir.listFiles();
		List filesDirs = Arrays.asList(filesAndDirs);
		for (int i = 0; i < filesDirs.size(); i++) {
			File file = (File) filesDirs.get(i);
			String filename = file.getName();
			String filepath = file.getPath();
			
			int pos = filepath.indexOf("image");
			String correctedimage = filepath.substring(pos);

			if (filterImage(correctedimage)) {
				//result.add(new WordImage(filepath, correctedimage)); //always
																	 // add,
																	 // even if
				WordImage wi = null;
				String key = StringManipulator.getOnlyCharacters(filename.substring(0, filename.indexOf(".")));	
				
				// directory
				if(map.containsKey(key)) {
					wi = (WordImage) map.get(key);
					wi.add(new WordImage(filename, correctedimage));
				}
				else {
					wi = new WordImage(filename, correctedimage);
				}
				map.put(key.toLowerCase(), wi);
			}

			if (!file.isFile()) {
				//must be a directory
				//recursive call!
				Map deeperList = getFileListingNoSort(file);
				map.putAll(deeperList);
				//result.addAll(deeperList);
			}
		}
		//return result;
		return map;
	}	
	
	private static boolean filterImage(String image) {
		if(image.indexOf("Thumbs.db") != -1) {
			return false;
		}
		if(image.indexOf(".") == -1) {
			return false;
		}
		
		return true;
	}
	
	// PRIVATE //
//	private static void getMappedFileListing(File aStartingDir, Map mapfiles)
//			throws FileNotFoundException {
//		List result = new ArrayList();
//		File[] filesAndDirs = aStartingDir.listFiles();
//		List filesDirs = Arrays.asList(filesAndDirs);
//		List base = new ArrayList();
//		for (int i = 0; i < filesDirs.size(); i++) {
//			
//			File file = (File) filesDirs.get(i);
//			result.add(file); //always add, even if directory
//			if (!file.isFile()) {
//				//must be a directory
//				//recursive call!
//				List deeperList = getFileListingNoSort(file);
//				//result.addAll(deeperList);
//				mapfiles.put(file.getName(), deeperList);
//			}
//			else {
//				base.add(file.getName());
//				mapfiles.put("basedirectory", base);
//			}
//		}
//		//return result;
//	}
	
	private static void getMappedFileListing(File aStartingDir, Map mapfiles) {
			//throws FileNotFoundException {
		List result = new ArrayList();
		File[] filesAndDirs = aStartingDir.listFiles();
		List filesDirs = Arrays.asList(filesAndDirs);
		//List base = new ArrayList();
		for (int i = 0; i < filesDirs.size(); i++) {

			File file = (File) filesDirs.get(i);
			result.add(file); //always add, even if directory
			if (!file.isFile()) {
				//must be a directory
				//recursive call!
				Map deeperList = getFileListingNoSort(file);
				//result.addAll(deeperList);
				mapfiles.put(file.getName(), deeperList);
			} else {
				//base.add(file.getName());
				//mapfiles.put("basedirectory", base);
			}
		}
		//return result;
	}
	

	/**
	 * Directory is valid if it exists, does not represent a file, and can be read.
	 */
	private static void validateDirectory(File aDirectory) {
			//throws FileNotFoundException {
		if (aDirectory == null) {
			throw new IllegalArgumentException("Directory should not be null.");
		}
		if (!aDirectory.exists()) {
			//throw new FileNotFoundException("Directory does not exist: "
				//	+ aDirectory);
		}
		if (!aDirectory.isDirectory()) {
			throw new IllegalArgumentException("Is not a directory: "
					+ aDirectory);
		}
		if (!aDirectory.canRead()) {
			throw new IllegalArgumentException("Directory cannot be read: "
					+ aDirectory);
		}
	}
	
}
